﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using SZHL.UI.AppCode;
using FastReflectionLib;
using System.Data;
using SZHL.Data;
using Newtonsoft.Json;

namespace SZHL.UI.API
{
    public class KJFSManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(KJFSManage).GetMethod(msg.Action.ToUpper());
            KJFSManage model = new KJFSManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加快捷方式
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDKJFS(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            JH_Auth_KJFS KJFS = JsonConvert.DeserializeObject<JH_Auth_KJFS>(P1);
            if (KJFS.Link.Trim() == "")
            {
                msg.ErrorMsg = "快捷方式连接不能为空";
                return;
            }
            if (KJFS.ID == 0)
            {
                KJFS.CRDate = DateTime.Now;
                KJFS.CRUser = UserInfo.User.UserName;
                new JH_Auth_KJFSB().Insert(KJFS);
            }
            else
            {
                new JH_Auth_KJFSB().Update(KJFS);
            }
            msg.Result = KJFS;
        }
        /// <summary>
        /// 获取快捷方式列表分页
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETUSERKJFSLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = string.Format(" CRUser='{0}' ", UserInfo.User.UserName);
            if (P1 != "")
            {
                strWhere += string.Format(" And title like  '%{0}%'", P1);
            }
            string tag = context.Request["tag"] ?? "";
            if (tag != "")
            {
                strWhere += string.Format(" And TagName='{0}'", tag);
            }
            int page = 0;
            int.TryParse(context.Request["p"] ?? "1", out page);
            int total = 0;
            DataTable dt = new ZH_XTGGB().GetDataPager("JH_Auth_KJFS", "*", 8, page, " CRDate desc", strWhere, ref total);
            msg.Result = dt;
            msg.Result1 = total;
        }
        /// <summary>
        /// 获取快捷方式列表不分页
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETUSERKJFSLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new JH_Auth_KJFSB().GetEntities(d => d.CRUser == UserInfo.User.UserName);
        }
        public void GETUSERKJFSINDEX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {

            string strSql = string.Format(" select * from JH_Auth_TAG  where CRUser='{0}' and model='KJFS'", UserInfo.User.UserName); 
            DataTable dt = new JH_Auth_TAGB().GetDTByCommand(strSql);
            List<JH_Auth_KJFS> KJFSList = new JH_Auth_KJFSB().GetEntities(d => d.CRUser == UserInfo.User.UserName).ToList();
            dt.Columns.Add("KJFS", Type.GetType("System.Object"));
            foreach(DataRow row in dt.Rows)
            {
                row["KJFS"] = KJFSList.Where(d => d.TagName == row["TagName"].ToString()).ToList();
            }
            msg.Result = dt;
        }
        /// <summary>
        /// 删除快捷方式
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELKJFSBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
             

                if (!new JH_Auth_KJFSB().Delete(d => d.ID.ToString() == P1))
                {

                    msg.ErrorMsg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取快捷方式信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETKJFSMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new JH_Auth_KJFSB().GetEntity(d => d.ID.ToString() == P1);
        }

    }
}